@isTest
private class BloodApplication2ControllerTest {
    static testMethod void testMethod1() {
         //客户1
        Account acc = TestUtils.createAccount();
        acc.Level__c = '三级甲等';
        insert acc;     

        //业务机会1
        Opportunity opp = TestUtils.createOpportunity();
        opp.AccountId = acc.Id;
        insert opp;

        Id pRT = Utils.getRecordTypeIdByDevNameAndObjName('NCProduct', 'Product__c');

    	Product__c p1 = TestUtils.createProduct();
        p1.Name = 'Test1';
        p1.NCCode__c = '100100100001'; 
        p1.RecordTypeId = pRT;
        p1.zl__c = '10';

        insert p1;

        Product__c p2 = TestUtils.createProduct();
        p2.Name = 'Test12';
        p2.NCCode__c = '100100100002'; 
        p2.RecordTypeId = pRT;
        p2.zl__c = '10';
        p2.MatchingReagents__c = p1.Id;
        insert p2;

	        

	   
        Contract__c con = new Contract__c();
        con.Account__c = acc.Id;
        con.Opportunity__c = opp.Id;
       	con.Status__c  = '已生效';
        con.Corpcode__c = 'MKSY';
        con.RecordTypeId = Utils.getRecordTypeIdByDevNameAndObjName('TerminalContract', 'Contract__c');
        insert con;


         ContractProduct__c cp = new ContractProduct__c();
        cp.Contract__c = con.Id;
        cp.Product__c =p2.Id  ;
        cp.Quantity__c = 5;
        cp.OriginNumber__c = 5;
        cp.SalesPrice__c = 1; 
        cp.CoopType__c = '正常销售';
        cp.ApplicationQuantity__c = 5;
        cp.Opportunity__c = opp.Id;
        insert cp ;


        PageReference page1 = new PageReference('/apex/BloodApplication2' );
        System.Test.setCurrentPage(page1);

        BloodApplication2Controller bac = new BloodApplication2Controller();
        bac.init();

        Map<String,String> searchMap = new Map<String,String>();
        searchMap.put('type', 'opp');
        searchMap.put('accId', acc.Id);
        bac.saveJson = EncodingUtil.urlEncode(JSON.serialize(searchMap),  'UTF-8');

       	bac.search();

       	 searchMap.put('type', 'account');
        bac.saveJson = EncodingUtil.urlEncode(JSON.serialize(searchMap),  'UTF-8');
       	 bac.search();


       	 searchMap.put('type', 'contract');
        bac.saveJson = EncodingUtil.urlEncode(JSON.serialize(searchMap),  'UTF-8');
       	 bac.search();



       	 List<Object> saveList =new List<Object>();
       	 Map<String,Object> saveMap =new Map<String,Object>();
       	 Map<String,String> baseMap =new Map<String,String>();

       	 baseMap.put('Company__c', '四川省迈克实业有限公司');
       	 baseMap.put('Account__c', acc.Id );
       	 baseMap.put('Opportunities__c', opp.Id);

       	 saveMap.put('type', Utils.getRecordTypeIdByDevNameAndObjName('PolicyWithoutContract', 'BloodSugarOrder__c'));
       	 saveMap.put('name', 'SalesmenCollect');

       	 saveList.add(new BloodApplication2Controller.BloodBeanOnly(p2));

       	 saveMap.put('base',baseMap );
       	 saveMap.put('datas', saveList);
        bac.saveJson = EncodingUtil.urlEncode(JSON.serialize(saveMap),  'UTF-8');
        bac.save();

        System.debug(bac.msg +' '+bac.backId);


        

          PageReference page3= new PageReference('/apex/BloodApplication2?id='+bac.backId );
        System.Test.setCurrentPage(page3);

        BloodApplication2Controller bac2 = new BloodApplication2Controller();
        bac2.init();

        searchMap.put('recordType', 'SalesmenCollect');
        bac2.saveJson = EncodingUtil.urlEncode(JSON.serialize(searchMap),  'UTF-8');
        bac2.searchProduct();

        searchMap.put('recordType', 'Replacement');
        bac2.saveJson = EncodingUtil.urlEncode(JSON.serialize(searchMap),  'UTF-8');
        bac2.searchProduct();

        searchMap.put('recordType', 'PolicyWithoutContract');
        bac2.saveJson = EncodingUtil.urlEncode(JSON.serialize(searchMap),  'UTF-8');
        bac2.searchProduct();


        update new BloodSugarOrder__c(Id=bac.backId,Status__c = '审批成功');

        PageReference page2 = new PageReference('/apex/BloodApplication2?id='+bac.backId+'&sync=true' );
        System.Test.setCurrentPage(page2);

        BloodApplication2Controller bac1 = new BloodApplication2Controller();
        bac1.init();
        bac1.saveJson =  EncodingUtil.urlEncode('是',  'UTF-8');
        bac1.syncToNc();
        System.debug(bac1.msg );

        update new BloodSugarOrder__c(Id=bac.backId,Synchronization__c=false,RecordTypeId =Utils.getRecordTypeIdByDevNameAndObjName('Give', 'BloodSugarOrder__c') );
        //bac1.bos.Synchronization__c = false;
        bac1.init();
		bac1.syncToNc();
        System.debug(bac1.msg );



    }

}